﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DynaPrecision.DataFitting
{
	internal class L2YMatrix<Tx, Ty> : LinearSystem.IMultiDimensionalArray<Ty>
	{
		public List<Func<Tx, Ty>> FunctionList;
		public Dictionary<Tx, Ty> XYs = new Dictionary<Tx, Ty>();

		public void AddXY(IEnumerable<Tx> x, IEnumerable<Ty> y)
		{
			var xx = x.ToList(); var yy = y.ToList();
			for (int i = 0; i < xx.Count && i < yy.Count; i++)
			{
				this.XYs.Add(xx[i], yy[i]);
			}
		}

		#region IMultiDimensionalArray<Ty> Members

		public Ty this[params int[] index]
		{
			get
			{
				return XYs.Aggregate(DefaultValueProvider<Ty>.Zero, (c, curr) => (c + curr.Value * (dynamic)FunctionList[index[0]](curr.Key)));
			}
			set
			{
				throw new NotImplementedException();
			}
		}

		public int GetLength(int DimensinNumber)
		{
			if (DimensinNumber == 0)
			{
				return FunctionList.Count;
			}
			else if (DimensinNumber == 1)
			{
				return 1;
			}
			else
			{
				return 0;
			}
		}

		public int Dimension
		{
			get { return 2; }
		}

		#endregion
	}
}